package com.ibeeking.found.msg.rest.service.impl;

import com.ibeeking.found.msg.rest.service.ISmsRecordService;
import com.ibeeking.found.msg.service.common.dos.SmsRecordDO;
import com.ibeeking.found.msg.service.mapper.SmsRecordMapper;
import com.ibeeking.nematos.msg.sms.aliyun.dto.SmsResultDto;
import com.ibeeking.nematos.utils.json.JsonUtils;
import com.ibeeking.nematos.utils.result.ResponseCodeEnum;
import com.ibeeking.nematos.utils.result.ResponseResult;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Map;

/**
 * @ClassName RecordServiceImpl
 * @Description
 * @Author ibeeking
 * @Date 2020-12-10 10:33
 **/
@Service
public class SmsRecordServiceImpl extends ServiceImpl<SmsRecordMapper, SmsRecordDO> implements ISmsRecordService {

    @Resource
    private SmsRecordMapper smsRecordMapper;

    @Override
    public void smsMsgRecord(Integer tenantId, Long typeId, String receiver, Map<String, String> param, ResponseResult<SmsResultDto> responseResult) {
        SmsRecordDO smsRecordDO = new SmsRecordDO();
        smsRecordDO.setTenantId(tenantId);
        smsRecordDO.setMsgWays(1);
        smsRecordDO.setMsgTypeId(typeId);
        smsRecordDO.setMsgCount(receiver.split(",").length);
        smsRecordDO.setMsgReceiver(receiver);
        smsRecordDO.setMsgRequestBody(JsonUtils.toJsonStr(param));
        smsRecordDO.setMsgResponseBody(JsonUtils.toJsonStr(responseResult));
        if (ResponseCodeEnum.SUCCESS.getCode().equals(responseResult.getCode())) {
            smsRecordDO.setMsgStatus(1);
        } else {
            smsRecordDO.setMsgStatus(0);
        }
        smsRecordDO.setCreateTime(LocalDateTime.now());
        smsRecordMapper.insert(smsRecordDO);
    }
}
